package com.sylvek.sentinel;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.SystemClock;
import android.preference.PreferenceManager;

/**
 * @author sylvek
 * 
 */
public class RequestLocationReceiver extends BroadcastReceiver {

    public static PendingIntent updateLocation = null;

    @Override
    public void onReceive(Context context, Intent intent)
    {
        final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
        if (pref.getBoolean(SettingsActivity.SETTING_LOCALIZABLE, false)) {

            if (RequestLocationReceiver.updateLocation == null) {
                final Intent updateLocation = new Intent(context, UpdateLocationService.class);
                RequestLocationReceiver.updateLocation = PendingIntent.getService(context, 0, updateLocation,
                        PendingIntent.FLAG_UPDATE_CURRENT);

            }

            final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),
                    AlarmManager.INTERVAL_FIFTEEN_MINUTES, RequestLocationReceiver.updateLocation);
        }
    }

}